/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : Word.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 21, 2008       Raghu Sarraff			       Created
 */
package lwf.handler.entities.english.word;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.string.util.StringManipulator;


/**
 * Type the class description here
 *  
 */
public class Word {
	
	private String name;
	private String type;
	private String sentencedata;

	/**
	 * @param name
	 * @param type
	 * @param sentences
	 */
	public Word(String name, String type, String sentencedata) {
		this.name = name;
		this.type = type;
		this.sentencedata = sentencedata;
	}
	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return Returns the type.
	 */
	public String getType() {
		return type;
	}
	
	//-----------------------------------------------------------------------------------
	// Exposed methods
	//-----------------------------------------------------------------------------------
	private List sentences;
	private String sentence;
	
	/**
	 * @return Returns the sentence.
	 */
	public String getSentence() {
		if(getSentences() != null) {
			int randnum = RandomNumberGenerator.generate(1, sentences.size());
			sentence = (String) sentences.get(randnum-1);
			return StringManipulator.replaceAWord(sentence, this.name, "<u>" + this.name + "</u>");
		}
		return sentence;
	}
	/**
	 * @return Returns the sentences.
	 */
	public List getSentences() {
		if(sentences == null) {
			processSentences();
		}
		return sentences;
	}
	
	private void processSentences() {
		if(this.sentencedata != null && sentencedata.trim().length() != 0) {
    		StringTokenizer stk = new StringTokenizer(sentencedata.trim(), "*");
    		if(sentences == null) {
    			sentences = new ArrayList(stk.countTokens());
    		}
       		while(stk.hasMoreTokens()) {
        		sentences.add(stk.nextToken());
       		}    			
		}
	}
	
	
}

